#define Sys_Global
#include "stm8s_conf.h"
#include "system.h"


Comm_EXT COMM_PARA Comm_Para;




void system_init(void)
{	
	
	CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV2);//MCU MAIN CLOCK=8MHZ 
	GPIO_Init(DATA_PORT,DATA_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_WriteHigh(DATA_PORT,DATA_PIN);

	GPIO_Init(LoadSwitch_PORT,LoadSwitch_PIN,GPIO_MODE_OUT_PP_LOW_SLOW);
	GPIO_WriteHigh(LoadSwitch_PORT,LoadSwitch_PIN);
	
	/*******************Timer1 Config********************************************************/
	TIM1_DeInit();
	TIM1_TimeBaseInit(TIM1_Prescaler_8,TIM2_PSCRELOADMODE_UPDATE,Tim1_Period_10MS,0);   //10ms
	TIM1_ITConfig(TIM1_IT_UPDATE,ENABLE);
	TIM1_UpdateDisableConfig(DISABLE);
	TIM1_UpdateRequestConfig( TIM1_UPDATESOURCE_GLOBAL);
	TIM1_Cmd(ENABLE);
	/*******************Timer4 Config********************************************************/
	TIM4_DeInit();
	TIM4_TimeBaseInit( TIM4_PRESCALER_8 ,Delay100us-1);///100us
	TIM4_PrescalerConfig(TIM4_PRESCALER_8,TIM4_PSCRELOADMODE_IMMEDIATE);
	TIM4_ARRPreloadConfig(ENABLE);
	TIM4_ITConfig( TIM4_IT_UPDATE , ENABLE);
	TIM4_UpdateDisableConfig(DISABLE);
	TIM4_UpdateRequestConfig( TIM4_UPDATESOURCE_GLOBAL);
	TIM4_Cmd(ENABLE );///used for decode handler
}


#if 0
unsigned char test;
unsigned char TestMode(SYSTEM_PARA * System_Para)
{
	SP_BEGIN(&(System_Para->sp_thd));
	SendData_Handler();

	if(Comm_Para.Comm_Status.DataSendSuccess==TRUE)
	{
		TimeOut_Record(&(System_Para->timer), 10);//100ms timeout
		SP_WAIT_UNTIL(&System_Para->sp_thd, (TimeOutDet_Check(&(System_Para->timer))));
		TimeOut_restart(&(System_Para->timer));
		Comm_Para.Comm_Status.DataSendSuccess=FALSE;
	}
	
	SP_END(&((*System_Para).sp_thd));
}



unsigned char System_Handler(SYSTEM_PARA * System_Para)
{
	switch(System_Para->SysWork_Sts)
	{
		case SleepMode:
		{
			TestMode(System_Para);
			break;
		}
		default:
		{
			System_Para->SysWork_Sts=SleepMode;
			break;
		}
	}
	
}
#endif